package com.wlyuan.open.constants;

import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lijing
 * 1,000纳克（ng）= 1微克（ug）
 * 1,000微克（ug）= 1毫克（mg）
 * 1,000毫克（mg）=1克（g）
 * 1,000克（g）=1千克（kg）
 * 1,000千克（kg）=1吨（t）
 */
@Getter
public enum WeightUnit {

    KG("千克", 1),
    T("吨", 2);

    private String name;
    private Integer value;

    WeightUnit(String name, Integer value) {
        this.value = value;
        this.name = name;
    }

    private static final Map<Integer, WeightUnit> MAP = Arrays.asList(WeightUnit.values()).stream().collect(Collectors.toMap(WeightUnit::getValue, Function.identity()));

    public static WeightUnit fromValue(Integer value) {
        return Optional.ofNullable(value).map(a -> Optional.ofNullable(MAP.get(a)).orElse(KG)).orElse(KG);
    }

}
